package com.zzh.ethernetstaticip;

import android.content.Context;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.EthernetManager;
import android.os.Bundle;
import android.os.PowerManager;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.blankj.utilcode.util.NetworkUtils;
import com.blankj.utilcode.util.RegexUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.blankj.utilcode.util.Utils;
import com.zzh.ethernetstaticip.receiver.MainFragmentReceiver;
import com.zzh.ethernetstaticip.utils.IpGetUtil;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private static final String TAG = "MainActivity";
    TextView tvIp;
    Button btnDhcp;
    Button btnStatic, btn_ping;
    EditText ip_et, netmask_et, gateway_et, dns1_et, dns2_et, ping_et;
    private MainFragmentReceiver fragReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Utils.init(getApplicationContext());
        fragReceiver = new MainFragmentReceiver();
        registerBroadcastReceiver();
        initView();

        IpGetUtil.getStaticIpConfiguration(this);
    }

    private void initView() {

        ip_et = findViewById(R.id.ip_et);
        netmask_et = findViewById(R.id.netmask_et);
        gateway_et = findViewById(R.id.gateway_et);
        dns1_et = findViewById(R.id.dns1_et);
        dns2_et = findViewById(R.id.dns2_et);

        tvIp = findViewById(R.id.tv_ip);
        btnDhcp = findViewById(R.id.btn_set_dhcp_ip);
        btnStatic = findViewById(R.id.btn_set_static_ip);
        btn_ping = findViewById(R.id.btn_ping);
        ping_et = findViewById(R.id.ping_et);
        btnDhcp.setOnClickListener(this);
        btnStatic.setOnClickListener(this);
        btn_ping.setOnClickListener(this);
        String ip = IpGetUtil.getIpAddress(MainActivity.this);
        tvIp.setText(getString(R.string.ip_address, ip));
    }

    @Override
    protected void onStart() {
        super.onStart();
        tvIp.setText(getString(R.string.ip_address, IpGetUtil.getIpAddress(this)));
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        switch (id) {
            case R.id.btn_set_dhcp_ip:
                boolean success = IpGetUtil.setEthernetIP(MainActivity.this, "DHCP",
                        "", "", "", "", "");
                if (success)
                    rebootSystem();
                break;
            case R.id.btn_set_static_ip:
                String ipAddress = ip_et.getText().toString().trim();
                String gateway = gateway_et.getText().toString().trim();
                String netmask = netmask_et.getText().toString().trim();
                String dns1 = dns1_et.getText().toString().trim();
                String dns2 = dns2_et.getText().toString().trim();


                if (!RegexUtils.isIP(ipAddress)) {
                    ToastUtils.showShort(getString(R.string.ip_error));
                }

                boolean success2 = IpGetUtil.setEthernetIP(MainActivity.this, "STATIC",
                        ipAddress, netmask,
                        gateway, dns1, dns2);
                Toast.makeText(this, success2 + "", Toast.LENGTH_SHORT).show();
                if (success2)
                    rebootSystem();
                break;
            case R.id.btn_ping:
                NetworkUtils.isAvailableByPingAsync(ping_et.getText().toString().trim(), new Utils.Callback<Boolean>() {
                    @Override
                    public void onCall(final Boolean data) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(MainActivity.this, "ping result:" + data, Toast.LENGTH_SHORT).show();
                            }
                        });
                    }
                });
                break;
        }
    }

    public void rebootSystem() {
        PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
        pm.reboot(null);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        getApplicationContext().unregisterReceiver(fragReceiver);
    }

    private void registerBroadcastReceiver() {
        IntentFilter intentFilter = new IntentFilter();
        // for net state changed
        intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);

        if (fragReceiver != null) {
            getApplicationContext().registerReceiver(fragReceiver, intentFilter);
            setBroadcastReceiverListener();
            Log.i(TAG, "registerBroadcastReceiver finished");
        }
    }

    private void setBroadcastReceiverListener() {
        fragReceiver.setFragmentListener(new MainFragmentReceiver.FragmentListener() {
            @Override
            public void getNetState(int state) {
                Log.i(TAG, "getNetState state=" + state);
                if (state > MainFragmentReceiver.NETSTATUS_INAVAILABLE) {
                    String ip = IpGetUtil.getIpAddress(MainActivity.this);
                    tvIp.setText(getString(R.string.ip_address, ip));
                } else {
                    tvIp.setText(getString(R.string.ip_address, getString(R.string.no_network)));
                }
            }
        });
    }
}
